package com.treasure.web.listener;

import com.treasure.domain.domain.DocumentIndexStoreDomainService;
import com.treasure.web.command.event.DocumentUploadEvent;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * @author sunhongmin
 * @date 2023/10/21 10:35
 * @description
 */
@Component
@RequiredArgsConstructor
public class DocumentUploadListener {
    private final DocumentIndexStoreDomainService documentIndexStoreDomainService;

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    @Async
    public void onApplicationEvent(@NotNull DocumentUploadEvent documentUploadEvent) {
        documentUploadEvent.validate();
        documentIndexStoreDomainService.indexDocumentAndStore(documentUploadEvent.getUploadDocumentRequest(), documentUploadEvent.getFile(), documentUploadEvent.getFileName());
    }
}
